package com.dep.controller;

import com.dep.aspect.Log;
import com.dep.aspect.Token;
import com.dep.common.BeanValidator;
import com.dep.common.CodeIdEnum;
import com.dep.common.PageRequest;
import com.dep.common.ResponseData;
import com.dep.dto.req.StaffInsertReq;
import com.dep.dto.req.StaffReq;
import com.dep.dto.resp.StaffResp;
import com.dep.entity.Department;
import com.dep.entity.Staff;
import com.dep.service.StaffService;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

@RestController
@RequestMapping("/staff/")
@Api(tags = "员工接口(token和sign暂时不设置)")
public class StaffController {

    @Resource
    private StaffService staffService;

    @Log
    @ApiOperationSupport(author = "黄伶通")
    @GetMapping("/page")
    @ApiOperation(value = "分页查询员工信息")
    @Token
    public ResponseData<PageInfo<StaffResp>> findStaff(StaffReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return staffService.findStaffPage(req);
    }

    @Log
    @ApiOperationSupport(author = "黄伶通")
    @PostMapping("/insert")
    @ApiOperation(value = "录入员工")
    @Token
    public ResponseData<Staff> insertStaff(@RequestBody StaffInsertReq req) throws Exception{
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        return staffService.insertStaff(req);
    }
}
